//
//  CommentTableViewCell.m
//  Cunli
//
//  Created by 湛亚 on 15/9/14.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "CommentTableViewCell.h"
#import "UIImageView+EMWebCache.h"
#import "ConvertToCommonEmoticonsHelper.h"
#import "PointsManager.h"
#import "UIColor+Addition.h"
#import "CommentReplyList.h"
#import "TTTAttributedLabel.h"
#import "YMButton.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
//#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface CommentTableViewCell()<TTTAttributedLabelDelegate>{
    
}
@end

@implementation CommentTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier andComment:(CommentReplyList *)content{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        _headImg=[[UIImageView alloc]init];
        _headImg.frame=CGRectMake(15, 13, 40, 40);
        _headImg.layer.cornerRadius = 20;
        _headImg.clipsToBounds = YES;
        _headImg.userInteractionEnabled=YES;
        if ([PointsManager isDefaultTouXiangWithUrlstring:content.pic]) {
            _headImg.image=[UIImage imageNamed:@"default_touxiang.png"];
        }else{
            [_headImg sd_setImageWithURL:[NSURL URLWithString:content.pic] placeholderImage:[UIImage imageNamed:@"default_touxiang.png"]];
        }
        //头像点击事件
        UITapGestureRecognizer *headTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lookPersonInfo)];
        [_headImg addGestureRecognizer:headTap];
        [self.contentView addSubview:_headImg];
        
        UILabel *nameLab=[[UILabel alloc]initWithFrame:CGRectMake(65, 10, 22*(content.truename.length), 25)];
        nameLab.text=content.truename;
        nameLab.font=[UIFont systemFontOfSize:16];
        nameLab.textColor=[UIColor convertHexColorToUIColor:0x333333];
        [self.contentView addSubview:nameLab];
        
        float height=content.commentHeight;
        _commentLabel=[PointsManager createWithframe:CGRectMake(65, 38, SCREEN_WIDTH-80, height) andString:content.content];
        _commentLabel.delegate=self;
        [self.contentView addSubview:_commentLabel];
        
        height=height+42;
        
        for (int i=0; i<content.replylist.count; i++) {
            
            NSDictionary *replyDic=[content.replylist objectAtIndex:i];
            ReplyComment *replyComment=[ReplyComment yy_modelWithDictionary:replyDic];
            
            NSString *replyString=[NSString stringWithFormat:@"%@回复%@:%@",replyComment.truename,replyComment.totruename,replyComment.content];
            NSString *commentReplyText = [ConvertToCommonEmoticonsHelper
                                          convertToSystemEmoticons:replyString];
            
            float replyHeight=[self heightWithString:commentReplyText];
            
            NSMutableAttributedString *str= [[NSMutableAttributedString alloc] initWithString:commentReplyText attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16],NSKernAttributeName : [NSNull null]}];
            //颜色
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor  convertHexColorToUIColor:0x333333] range:NSMakeRange(0,commentReplyText.length)];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:18/255.0 green:133/255.0 blue:210/255.0 alpha:1] range:NSMakeRange(0,replyComment.truename.length)];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:18/255.0 green:133/255.0 blue:210/255.0 alpha:1] range:NSMakeRange(replyComment.truename.length+2,replyComment.totruename.length)];
            
            //行间距
            NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
            [paragraphStyle setLineSpacing:3];
            [str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,commentReplyText.length)];
            
            TTTAttributedLabel *ttlabel=[PointsManager createWithframe:CGRectMake(65, height-3, SCREEN_WIDTH-80, replyHeight) andNSMutableAttributedString:str andNsstring:commentReplyText];
            ttlabel.delegate=self;
            ttlabel.tag=i;
            ttlabel.userInteractionEnabled=YES;
            //ttlabel.backgroundColor=[UIColor greenColor];
            
            //[ttlabel addLinkToTransitInformation:[NSDictionary dictionaryWithObject:replyComment forKey:@"value"] withRange:NSMakeRange(0, replyComment.truename.length)];
            
            //[ttlabel addLinkToTransitInformation:[NSDictionary dictionaryWithObject:replyComment forKey:@"value1"] withRange:NSMakeRange(replyComment.truename.length+2,replyComment.totruename.length)];
            
            //评论点击事件
            UITapGestureRecognizer *commentTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(commentTap:)];
            [ttlabel addGestureRecognizer:commentTap];
            
            [self.contentView addSubview:ttlabel];
            
            height=height+replyHeight;
        }
        
        _timeLabel=[[UILabel alloc]init];
        _timeLabel.textColor=[UIColor convertHexColorToUIColor:0x999999];
        _timeLabel.text=content.addtime;
        _timeLabel.font=[UIFont systemFontOfSize:13];
        _timeLabel.frame=CGRectMake(65, height, 120, 20);
        [self.contentView addSubview:_timeLabel];
        
        _moreButton=[YMButton buttonWithType:UIButtonTypeCustom];
        _moreButton.frame=CGRectMake(SCREEN_WIDTH-50, height, 40, 20);
        [_moreButton setImage:[UIImage imageNamed:@"morebuttonimg.png"] forState:0];
        [self.contentView addSubview:_moreButton];
    }
    return self;
}

-(float)heightWithString:(NSString *)introduce{
    
    CGRect rect = [introduce boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-90, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0f]} context:nil];
    CGFloat height = ceilf(rect.size.height);
    return height+5;
}

-(void)lookPersonInfo{
    
    _goToPersonInfo(_selectedIndexPath.row);
}

-(void)commentTap:(UIGestureRecognizer *)gestureView{
    
    NSLog(@"%ld",gestureView.view.tag);
    _replyComment(_selectedIndexPath.row,gestureView.view.tag);
}

#pragma mark - TTTAttributedLabelDelegate 点击网址中的超链接
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    
    NSLog(@">>>>>>%@",url);
    NSString *urlString=[url absoluteString];
    NSRange foundObj=[urlString rangeOfString:@"http://" options:NSCaseInsensitiveSearch];
    if(foundObj.length>0) {
        [[UIApplication sharedApplication] openURL:url];
    }else{
        urlString=[NSString stringWithFormat:@"http://%@",urlString];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }
    return;
}

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTransitInformation:(NSDictionary *)components{
    
    //    if ([components objectForKey:@"value"]) {
    //        _replyComment([components objectForKey:@"value"],_selectedIndexPath.row,NO);
    //    }else{
    //        _replyComment([components objectForKey:@"value1"],_selectedIndexPath.row,YES);
    //    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
